home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
OS
/
ZNotify.cpp
< prev
next >
Wrap
Text File
|
1997-08-07
|
5KB
|
208 lines
/*
* File: ZNotify.cpp
* Summary: A wrapper around the Notification Manager.
* Written by: Jesse Jones
*
* Copyright ゥ 1996 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <-> 1/09/96 JDJ Created
*/
#include <ZNotify.h>
#include <Icons.h>
#include <Resources.h>
#include <ZConstants.h>
#include <ZExceptions.h>
#include <ZMiscUtils.h>
#include <ZProcess.h>
#include <ZStringUtils.h>
// ===================================================================================
// class TNotify
// ===================================================================================
//----------------------------------------------------------------
//
// TNotify::~TNotify
//
//----------------------------------------------------------------
TNotify::~TNotify()
{
if (mInstalled)
NMRemove(&mRecord);
if (mRecord.nmIcon != nil)
DisposeIconSuite(mRecord.nmIcon, false);
if (mRecord.nmSound != nil && mRecord.nmSound != (Handle) -1)
ReleaseResource(mRecord.nmSound);
if (mRecord.nmResp != nil)
DisposeRoutineDescriptor(mRecord.nmResp);
}
//----------------------------------------------------------------
//
// TNotify::TNotify (ResID, ResID, bool)
//
//----------------------------------------------------------------
TNotify::TNotify(ResID iconID, ResID soundID, bool useMark)
{
this->Init("", iconID, soundID, useMark);
}
//----------------------------------------------------------------
//
// TNotify::TNotify (string, ResID, ResID, bool)
//
//----------------------------------------------------------------
TNotify::TNotify(const string& mesg, ResID iconID, ResID soundID, bool useMark)
{
this->Init(mesg, iconID, soundID, useMark);
}
//----------------------------------------------------------------
//
// TNotify::TNotify (string, ResID)
//
//----------------------------------------------------------------
TNotify::TNotify(const string& mesg, ResID soundID)
{
this->Init(mesg, 0, soundID, false);
}
//----------------------------------------------------------------
//
// TNotify::Init
//
//----------------------------------------------------------------
void TNotify::Init(const string& mesg, ResID iconID, ResID soundID, bool useMark)
{
mRecord.nmIcon = nil;
mRecord.nmSound = nil;
mRecord.nmResp = nil;
try {
mText = StrToPStr(mesg);
mInstalled = false;
mCalledFromForeground = UProcess::InFront();
// Initialize the notification manager record.
mRecord.qLink = nil;
mRecord.qType = nmType;
mRecord.nmMark = useMark;
mRecord.nmStr = mesg != "" ? mText : nil;
mRecord.nmRefCon = (long) this;
// Create a handle for the icon.
if (iconID > 0) {
(void) GetIconSuite(&mRecord.nmIcon, iconID, svAllSmallData);
ASSERT(mRecord.nmIcon != nil);
if (mRecord.nmIcon != nil) // not worth throwing an exception for
HNoPurge(mRecord.nmIcon);
}
// Create a handle for the sound.
if (soundID > 0) {
mRecord.nmSound = GetResource('snd ', soundID);
ASSERT(mRecord.nmSound != nil);
if (mRecord.nmSound != nil) // not worth throwing an exception for
HNoPurge(mRecord.nmSound);
} else if (soundID < 0)
mRecord.nmSound = (Handle) -1;
// Create a upp for the notification callback.
mRecord.nmResp = NewNMProc(TNotify::DoCallback);
ThrowIfMemFail(mRecord.nmResp);
// Register with the state broadcaster so we can find out when we're
// switched back in (we have to wait until we're switched to the front
// before we remove the notification since we want the icon to keep
// blinking).
if (!mCalledFromForeground)
TStateBroadcaster::Instance()->AddListener(this);
} catch (...) {
// this can be constructed from dicy places like Drag Manager callbacks
// so we'll eat all exceptions
}
}
//----------------------------------------------------------------
//
// TNotify::Post
//
//----------------------------------------------------------------
void TNotify::Post()
{
ASSERT(!mInstalled);
OSErr err = NMInstall(&mRecord);
ASSERT(err == noErr);
mInstalled = err == noErr;
}
//----------------------------------------------------------------
//
// TNotify::HandleCallback
//
//----------------------------------------------------------------
void TNotify::HandleCallback()
{
this->OnCallback();
if (mCalledFromForeground)
delete this;
}
//----------------------------------------------------------------
//
// TNotify::OnBroadcast
//
//----------------------------------------------------------------
void TNotify::OnBroadcast(const SStateMessage& message)
{
ASSERT(!mCalledFromForeground);
if (message.mesg == kResumingApp)
delete this;
}
//----------------------------------------------------------------
//
// TNotify::DoCallback [static]
//
//----------------------------------------------------------------
pascal void TNotify::DoCallback(NMRec* record)
{
try {
TNotify* thisPtr = reinterpret_cast<TNotify*>(record->nmRefCon);
thisPtr->HandleCallback();
} catch (...) {
// can't throw in a callback!
}
}